package com.fr.plugin.provider;

import com.fr.db.fun.impl.AbstractDBAccessProvider;
import com.fr.log.FineLoggerFactory;
import com.fr.plugin.provider.entity.VcsAuthDao;
import com.fr.plugin.provider.entity.VscAuth;
import com.fr.plugin.provider.req.QueryVcsListReq;
import com.fr.record.analyzer.EnableMetrics;
import com.fr.report.dao.VcsDAO;
import com.fr.report.entity.VcsEntity;
import com.fr.stable.StringUtils;
import com.fr.stable.db.accessor.DBAccessor;
import com.fr.stable.db.action.DBAction;
import com.fr.stable.db.dao.BaseDAO;
import com.fr.stable.db.dao.DAOContext;
import com.fr.stable.db.dao.DAOProvider;
import com.fr.stable.query.QueryFactory;
import com.fr.stable.query.condition.QueryCondition;
import com.fr.stable.query.restriction.RestrictionFactory;

import java.util.List;


@EnableMetrics
public class ExtendAccessProvider extends AbstractDBAccessProvider {

    private static DBAccessor accessor;

    public static DBAccessor getAccessor() {
        return accessor;
    }

    @Override
    public DAOProvider[] registerDAO() {
        return new DAOProvider[]{
               new DAOProvider() {
                   @Override
                   public Class getEntityClass() {
                       return VscAuth.class;
                   }

                   @Override
                   public Class<? extends BaseDAO> getDAOClass() {
                       return VcsAuthDao.class;
                   }
               },
               new DAOProvider() {
                   @Override
                   public Class getEntityClass() {
                       return VcsEntity.class;
                   }

                   @Override
                   public Class<? extends BaseDAO> getDAOClass() {
                       return VcsDAO.class;
                   }
               }
        };
    }

    @Override
    public void onDBAvailable(DBAccessor accessor) {
        ExtendAccessProvider.accessor = accessor;
    }


    public static void addVscAuth( final VscAuth bean ){
        try{
            accessor.runDMLAction(new DBAction<Boolean>() {
                @Override
                public Boolean run(DAOContext context) throws Exception {
                    context.getDAO(VcsAuthDao.class).add(bean);
                    return true;
                }
            });
        }catch(Throwable e){
            FineLoggerFactory.getLogger().error(e.getMessage(), e);
        }
    }

    public static VscAuth queryVcsByFileName(String fileName){
        try{
           return accessor.runQueryAction(new DBAction<VscAuth>() {
                @Override
                public VscAuth run(DAOContext context) throws Exception {
                    QueryCondition condition = QueryFactory.create();
                    condition.addRestriction(RestrictionFactory.eq("fileName", fileName));
                    return context.getDAO(VcsAuthDao.class).findOne(condition);
                }
            });
        }catch (Exception e){
            FineLoggerFactory.getLogger().error(e.getMessage(), e);
        }
       return null;
    }


    /***
     * 删除文件权限
     * @param id
     * @return
     */
    public static boolean deleteVcs(String id){
        try{
            return accessor.runQueryAction(new DBAction<Boolean>() {
                @Override
                public Boolean run(DAOContext context) throws Exception {
                    context.getDAO(VcsAuthDao.class).remove(id);
                    return true;
                }
            });
        }catch (Exception e){
            FineLoggerFactory.getLogger().error(e.getMessage(), e);
        }
        return false;
    }




    /***
     * 查询文件的总页数
     * @param req
     * @return
     */
    public static Long queryVcsAuthCount(QueryVcsListReq req){
        try{
            return accessor.runQueryAction(new DBAction<Long>() {
                @Override
                public Long run(DAOContext context) throws Exception {
                    QueryCondition condition = QueryFactory.create();
                    return context.getDAO(VcsAuthDao.class).count(condition);
                }
            });
        }catch (Exception e){
            FineLoggerFactory.getLogger().error(e.getMessage(), e);
        }
        return null;
    }

    /***
     * 查询配置对应的权限列表
     * @return
     */
    public static List<VscAuth> queryVcsAuthList(QueryVcsListReq req){
        try{
            return accessor.runQueryAction(new DBAction<List<VscAuth>>() {
                @Override
                public List<VscAuth> run(DAOContext context) throws Exception {
                    QueryCondition condition = QueryFactory.create();
                    condition.setSkip((req.getPage()-1)*req.getLimit());
                    condition.setCount(req.getLimit());
                    return context.getDAO(VcsAuthDao.class).find(condition);
                }
            });
        }catch (Exception e){
            FineLoggerFactory.getLogger().error(e.getMessage(), e);
        }
        return null;
    }










    /***
     * 查询文件列表
     * @return
     */
    public static List<VcsEntity> queryVcsList(){
        try{
            return accessor.runQueryAction(new DBAction<List<VcsEntity>>() {
                @Override
                public List<VcsEntity> run(DAOContext context) throws Exception {
                    QueryCondition condition = QueryFactory.create();
                    return context.getDAO(VcsDAO.class).find(condition);
                }
            });
        }catch (Exception e){
            FineLoggerFactory.getLogger().error(e.getMessage(), e);
        }
        return null;
    }






    /***
     * 查询文件的总页数
     * @param req
     * @return
     */
    public static Long queryVcsCount(QueryVcsListReq req){
        try{
            return accessor.runQueryAction(new DBAction<Long>() {
                @Override
                public Long run(DAOContext context) throws Exception {
                    return context.getDAO(VcsDAO.class).count( QueryFactory.create());
                }
            });
        }catch (Exception e){
            FineLoggerFactory.getLogger().error(e.getMessage(), e);
        }
        return null;


    }





    /***
     * 查询指定的文件
     * @param fileName
     * @return
     */
    public static VcsEntity getByFileName(String fileName){
        try{
            return accessor.runQueryAction(new DBAction<VcsEntity>() {
                @Override
                public VcsEntity run(DAOContext context) throws Exception {
                    QueryCondition condition = QueryFactory.create();
                    condition.addRestriction(RestrictionFactory.eq("filename", fileName));
                    return context.getDAO(VcsDAO.class).findOne(condition);
                }
            });
        }catch (Exception e){
            FineLoggerFactory.getLogger().error(e.getMessage(), e);
        }
        return null;
    }



    public static void update(VscAuth bean ){
        try{
            accessor.runDMLAction(new DBAction<Boolean>() {
                @Override
                public Boolean run(DAOContext context) throws Exception {
                    context.getDAO(VcsAuthDao.class).update(bean);
                    return true;
                }
            });
        }catch(Throwable e){
            FineLoggerFactory.getLogger().error(e.getMessage(), e);
        }
    }








}
